var createStore = require('./redux/createStore')
var combineReducers = require('./redux/combineReducers')
var applyMiddleware = require('./redux/applyMiddleware')

var thunk = require('./redux-thunk')

var initState = {
    name: '空着呢'
}

function reducer1(state = initState, action) {
    if (action.type == 'reducer1/setState') return { ...state, ...action.payload }
    return state
}
function reducer2(state = initState, action) {
    if (action.type == 'reducer2/setState') return { ...state, ...action.payload }
    return state
}
function reducer3(state = initState, action) {
    if (action.type == 'reducer3/setState') return { ...state, ...action.payload }
    return state
}

// reducers函数的整合
var reducers = { reducer1, reducer2, reducer3 }

let store = createStore(combineReducers(reducers),applyMiddleware(thunk))

var listener = store.subscribe(() => {
    console.log('执行我了啊')
})

function getData(){
    return (dispatch,state)=>{
        setTimeout(()=>{
            dispatch({ type: 'reducer1/setState', payload: { name: '小白' } })
            console.log(store.getState())
        },2000)
    }
}

store.dispatch(getData())
